// pages/pulish/pulish.js
var app = getApp()
const db = wx.cloud.database()
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text: '',
    imgUrl: '',
    count: 0,
    time: '',
    border: true,
    block1:['医疗','火灾','地震','水灾','泥石流'],
    block2:''
  },

  navigateBack: function () {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCount()
    this.setData({
      border: app.globalData.check == 1 ? true : false
    })

  },

  changeBlock: function (e) {
    var i = e.detail.value
    block=this.data.block1[i]
    this.setData({
      block2: block
    })
  },
  onShow: function () {
    let username = app.globalData.username
    if (!username) {
      wx.showToast({
        title: '您还未登录,请先登录~',
        icon: 'none'
      })

      setTimeout(() => {
        wx.switchTab({
          url: '../../pages/infor/infor',
        })
      }, 3000)
    } else {
      console.log(username)
    }
    db.collection("eas-count")
      .where({
        _id: '381d149061b5b309021452530f992faa'
      })
      .get({})
      .then(res => {
        console.log("获取账号成功", res);
        getApp().globalData.imgid = res.data[0].imgcount
        const id = getApp().globalData.imgid
        console.log(id)

      })
  },
  getCount: function () {
    //已输入的字数
    var that = this
    db.collection('eas-qzxx').count({
      success: res => {
        that.setData({
          count: Number(res.total) + 1
        })
      }
    })

  },
  textInput: function (e) {
    this.setData({
      text: e.detail.value
    })
  },
  publish: function () {
    var year = new Date().getFullYear() // 示例
    var month = new Date().getMonth() + 1
    var day = new Date().getDate()
    var hours = new Date().getHours()
    var minutes = new Date().getMinutes()
    var seconds = new Date().getSeconds()
    const time = time
    this.setData({
      time: year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds
    })
    var data = {
      image: this.data.imgSrc, //将图片储存为数组类型
      content: this.data.text, //用户输入的文字
      comment: Number,
      block: this.data.block2,
      username: app.globalData.username, //用户名
      formId: Number(this.data.formId) + 1, //是现在数据库的条数+1
      time: year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds
    }
    //validStatus: 审核状态, 通过时候 +1, 反对时候-1
    //validTime: 审核次数, 最多5次,如果反对的人大于等于3,则不通过

    console.log(data)

    if (data.content) {

      db.collection('eas-qzxx').add({
        data: data,
        success: res => {
          wx.showToast({
            title: '发布成功',
          })
          setTimeout(() => {

            wx.switchTab({
              url: '../home/home',
            })
          }, 1000)
        },
        fail: e => {
          wx.showToast({
            title: '发布错误',
          })
          console.log(e)
        }
      })
    } else {
      wx.showToast({
        title: '请填写文字',
        icon: 'none'
      })
    }

  },


  uploadImg() {
    wx.showLoading({
      title: '',
    });
    this.inc()
    // 让用户选择一张图片
    wx.chooseImage({
      count: 1,
      success: chooseResult => {
        // 将图片上传至云存储空间
        var imgcount = app.globalData.imgid
        wx.cloud.uploadFile({
          // 指定上传到的云路径
          cloudPath: "eas-qzxx/" + imgcount + '.png' + 'imgset2',
          // 指定要上传的文件的小程序临时文件路径
          filePath: chooseResult.tempFilePaths[0],
          config: {
            env: this.data.envId
          }
        }).then(res => {
          console.log('上传成功', res);
          this.setData({
            haveGetImgSrc: true,
            imgSrc: res.fileID
          });
          wx.hideLoading();
        }).catch((e) => {
          console.log(e);
          wx.hideLoading();
        });
      },
    });
  },

  clearImgSrc() {
    this.setData({
      haveGetImgSrc: false,
      imgSrc: ''
    });
  },

  inc: function () {
    db.collection('eas-qzxx').doc('287a53aa61b4b9440199b021494bee4b').update({
      data: {
        imgcount: _.inc(1)
      }

    })
    console.log('cg')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})